package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.service.*;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 老韦
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-08-16 18:28:23
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomFacilityService roomFacilityService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    PaymentTypeService paymentTypeService;



    @Override
    public IPage<RoomItemVo> listPageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.listPageRoomItemByQuery(page,queryVo);
    }

    @Override
    public void listsaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        Boolean flag = roomSubmitVo.getId()!=null;
        RoomInfo roomInfo = roomSubmitVo;
        super.saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId(); //拿出房间的id用来删除
        if (flag){
            //有房间id 说明是修改房间信息 先删除 后面添加
            //先删除图片
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);

            //删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> attrQueryMapper = new LambdaQueryWrapper<>();
            attrQueryMapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(attrQueryMapper);
            //删除 配套信息列表
            LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityQueryWrapper);
            //删除 标签信息列表
            LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelQueryWrapper);
            //删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
            paymentQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentQueryWrapper);
            //删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
            termQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(termQueryWrapper);

        }

        //插入或更新

        //更新图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        //非空判断 判断是否插入了图片
        if (!graphVoList.isEmpty()){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

        //更新属性信息列表
        List<Long> attrValueVoList = roomSubmitVo.getAttrValueIds();
        //非空判断
        if (!attrValueVoList.isEmpty()){
            List<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long aLong : attrValueVoList) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(id);
                roomAttrValue.setAttrValueId(aLong);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        //更新配套信息列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!facilityInfoIds.isEmpty()){
            List<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(id);
                roomFacility.setFacilityId(facilityInfoId);
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        //更新标签信息列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!labelInfoIds.isEmpty()){
            List<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setLabelId(labelInfoId);
                roomLabel.setRoomId(id);
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);

        }




        //更新支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!paymentTypeIds.isEmpty()){
            List<RoomPaymentType> roomPaymentTypes =new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(id);
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //更新租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!leaseTermIds.isEmpty()){
            List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTerm.setRoomId(id);
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }



    }

    @Override
    public RoomDetailVo listByDetailById(Long id) {

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //根据房间Id查询房间的配套信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //查公寓所属信息
        ApartmentInfo byId = apartmentInfoService.getById(roomDetailVo.getApartmentId());
        roomDetailVo.setApartmentInfo(byId);

        //查图片
        LambdaQueryWrapper<GraphInfo>  lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        lambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        List<GraphInfo> list = graphInfoService.list(lambdaQueryWrapper);
        ArrayList<GraphVo> graphVos = new ArrayList<>();
        for (GraphInfo graphInfo : list) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVos.add(graphVo);
        }
        roomDetailVo.setGraphVoList(graphVos);

        //查属性信息列表
        List<AttrValueVo> attrValueIds = roomAttrValueService.listAttrByRoomId(roomDetailVo.getId());
        //查配套属性信息列表
        List<FacilityInfo> facilityInfos = roomFacilityService.listFacilityInfosByRoomId(roomDetailVo.getId());
        //查标签信息列表
        List<LabelInfo> labelInfoList =roomLabelService.listLabelInfosByRoomId(roomDetailVo.getId());
        //查支付方式列表
        List<PaymentType> paymentTypeList=roomPaymentTypeService.listPaymentByRoomId(roomDetailVo.getId());
        //查可选租期列表
        List<LeaseTerm> leaseTermList=roomLeaseTermService.listLeaseTermByRoomId(roomDetailVo.getId());


        //将查询的数据放入要返回的对象
        roomDetailVo.setAttrValueVoList(attrValueIds);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }
}




